package com.zjitc.service.impl;

import com.sun.org.apache.bcel.internal.generic.NEW;
import com.zjitc.common.ServerResponse;
import com.zjitc.dao.UserDao;
import com.zjitc.model.Emp;
import com.zjitc.model.User;
import com.zjitc.redis.MyRedis;
import com.zjitc.token.Token;
import java.awt.event.ItemEvent;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Random;
import java.util.UUID;

import com.zjitc.token.Cache;
import javax.servlet.http.HttpSession;
import org.apache.ibatis.scripting.xmltags.ForEachSqlNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zjitc.service.UserService;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import sun.misc.Cleaner;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/3/28 15:44
 * @description:
 */
@Service
public class UserServiceImpl implements UserService {

  @Autowired
  private UserDao userDao;
  @Autowired
  private Jedis jedis;
  private JedisPool jedisPool;

  @Override
  public ServerResponse login(String username, String password) {
    int count = userDao.userNameCount(username);
    /*验证用户名是否存在 若不存在 返回信息*/
    if (count != 1) {
      ServerResponse serverResponse = ServerResponse.createError("The Username do not Exit !");
    }
    /*若用户名存在 验证密码正确性*/
    User user = userDao.checkByUsernameAndPassword(username, password);
    /*若密码错误*/
    if (!password.equals(user.getPassword())) {
      ServerResponse serverResponse = ServerResponse.createError("The Password is Error !");
    }
    user.setPassword(null);
    user.setQuestion(null);
    user.setAnswer(null);
    /*若成功*/
    return ServerResponse.createSuccess(user);
  }


  @Override
  public ServerResponse<User> userRegister(User user) {
    int count = userDao.userNameCount(user.getUsername());
    if (count != 0) {
      ServerResponse serverResponse = ServerResponse.createError("The Username is Exit !");
    }
    user.setRole(0);
    user.setCreate_time(new Date());
    user.setUpdate_time(new Date());
    userDao.userRegister(user);

    return ServerResponse.createSuccess(user);
  }

  @Override
  public ServerResponse<Object> check_valid(String str, String type) {

    if (type.equals("username")) {
      boolean userName = userDao.findUserName(str);
      if (userName == true) {
        return ServerResponse.createError("用户名已存在");
      }
      return ServerResponse.createSuccess("校验成功");
    } else {
      boolean email = userDao.findUserEmail(str);
      if (email == true) {
        return ServerResponse.createError("用户名已存在");
      }
      return ServerResponse.createSuccess("校验成功");
    }
  }

  @Override
  public ServerResponse get_user_info(User user) {
    if (user.getUsername() != null) {
      /*user.
      User user =userDao.findByUsername(username);*/
      return ServerResponse.createSuccess(user);
    }
    return ServerResponse.createError("用户未登录,无法获取当前用户信息");

  }

  @Override
  public ServerResponse forget_get_question(String username) {
    User user = userDao.findByUsername(username);
    if (user.getQuestion() == null) {
      return ServerResponse.createError("该用户未设置找回密码问题");
    }
    return ServerResponse.createError(user.getQuestion());
  }

  @Override
  public ServerResponse forget_check_answer(String username, String question, String answer) {
    User user = userDao.findByUsername(username);
    if (username.equals(user.getUsername())) {
      if (question.equals(user.getQuestion())) {
        if (answer.equals(user.getAnswer())) {
          Cache cache = new Cache();
          UUID uuid = UUID.randomUUID();
          String str = uuid.toString();
          cache.add(username, str);
          String temp = username+str;

          //HttpSession session =null;
          //session.setAttribute("token",temp);
          Jedis jedis = new Jedis();
          jedis.set(temp,"1");
          jedis.expire(temp,2);
          Token token = new Token();
          token.setCreateTime(LocalDateTime.now());
          token.setUuid(temp);
  /*        jedis.set(temp,"Hello World");
          jedis.expire(temp,2);*/

          return ServerResponse.createSuccess(cache);
        }
      }
    }
    return ServerResponse.createError("问题答案错误");
  }

  @Override
  public ServerResponse forget_reset_password(String username, String passwordOld,
      Token forgetToken) {
    User user = new User();
    //if (forgetToken==null){
    //  return ServerResponse.createError("token已经失效");
    //}else if{
    //  if (user.setUsername(username)){
    //
    //  }
    //}
    return null;
  }

  @Override
  public ServerResponse reset_password(User user, String passwordOld, String passwordNew) {
    //String password = userDao.equalPassword(passwordOld);
    User user1 = userDao.findByUsername(user.getUsername());
    String username = user.getUsername();
    if (user1.getPassword().equals(passwordOld)) {
      userDao.updatePassword(passwordNew, username);
      return ServerResponse.createSuccess("修改密码成功");
    }
    return ServerResponse.createError("旧密码输入错误");
  }

  @Override
  public ServerResponse update_information(User user, String email, String phone, String question,
      String answer) {
    if (user.getUsername() != null) {
      userDao.updateUser(email, phone, question, answer, user.getUsername());
      return ServerResponse.createSuccess("更新个人信息成功");
    }

    return ServerResponse.createError("用户未登录");
  }

  @Override
  public ServerResponse get_information(User user) {
    if (user.getUsername() != null) {
      userDao.checkByUsernameAndPassword(user.getUsername(), user.getPassword());
      return ServerResponse.createSuccess(user);
    }
    return ServerResponse.createError(10, "用户未登录,无法获取当前用户信息");
  }

  @Override
  public ServerResponse Emp(List<Emp> list) {

    //System.out.println(chars.charAt((int)(Math.random() * 2)));
    for (int i = 0; i < 5000; i++) {
      UUID uuid =UUID.randomUUID();
      String uu = String.valueOf(uuid);
      Random random =new Random();
      int age=random.nextInt(100);
      String chars = "wm";
      String sex = String.valueOf(chars.charAt((int)(Math.random() * 2)));
      Emp emp =new Emp(uu,age,sex);
      list.add(emp);
      Jedis jedis = new Jedis();
      HashMap<String,String > item = new HashMap<>();
      if (emp.getAge()>45){
        jedis.sadd("AGE_MAX_45", "");
      }
      if (i==4999){
        i=0;
      }
    }userDao.addEmps(list);

    return ServerResponse.createSuccess(list);
  }

/*  @Override
  public ServerResponse<List> findAllUser() {
    userDao.findAllUser();
    MyRedis redis = new MyRedis(jedisPool);
    redis.set();
  }*/


}
